package jrain.fw.dao.common.transaction;

import java.sql.Connection;
import java.util.Map;

import jrain.fw.core.utils.FwUtils;
import jrain.utils.sql.SqlUtils;

public class Connholder {

	private final Connection conn;

	public Connholder(String name, Map<String, Object> data) {
		conn = FwUtils.getDataSourceService().getConnection(name, data);
		SqlUtils.setAutoCommit(conn, false);
	}

	public Connection getConnection() {
		return conn;
	}

	public void commit() {
		SqlUtils.commit(conn);
		SqlUtils.setAutoCommit(conn, true);
		SqlUtils.closeConn(this.conn);
	}

	public void rollback() {
		SqlUtils.rollback(conn);
		SqlUtils.setAutoCommit(conn, true);
		SqlUtils.closeConn(this.conn);
	}

	public void close() {
		SqlUtils.closeConn(this.conn);
	}

	public void close(Connection conn) {
		if (conn == this.conn) {
			SqlUtils.closeConn(this.conn);
		} else {
			SqlUtils.closeConn(conn);
			SqlUtils.closeConn(this.conn);
		}
	}

}
